package cn.tansanqinger.controller.login_register;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.tansanqinger.service.verification.IVerificationService;



/**
 * 用于登陆，注册，找回密码的请求
 * @author C3H2
 * @time 9月
 */
@Controller
public class LoginRegisterController {
	
	@Autowired
	private IVerificationService verification;
	
	@RequestMapping("/login/register")
	public String register(HttpServletRequest request, Model model) {
		model = Model_Session(request, model);
		return "stair/register";
	}
	
	
	
	@RequestMapping("/login")
	public String login(HttpServletRequest request, Model model) {
		model = Model_Session(request, model);
		return "stair/login";
	}
	
	@RequestMapping("/login/passwd")
	public String passwd(HttpServletRequest request, Model model) {
		model = Model_Session(request, model);
		return "stair/passwd";
	}
	
	/**
	 * 将验证码图片显示到前端
	 * @param request 用于获取session
	 * @return 返回验证码的数据
	 */
	@GetMapping(value = "/login/code",produces = MediaType.IMAGE_JPEG_VALUE)
    @ResponseBody
	public byte[] image(HttpServletRequest request) {
		return verification.messageInit(request.getSession().getId());
	}
	
	/**
	 * 对数据进行装配
	 * @param request 用于获取session
	 * @param model 用于装配数据
	 * @return 返回装配结果
	 * @error 错误信息
	 * @message 成功信息
	 */
    public Model Model_Session(HttpServletRequest request, Model model) {
    	HttpSession session = request.getSession();
		model.addAttribute("error", session.getAttribute("error"));
		session.removeAttribute("error");
		model.addAttribute("message", session.getAttribute("message"));
		session.removeAttribute("message");
		return model;
    }

		
}
